package com.yao.service.impl;

import com.yao.util.SaveFile;
import com.yao.util.SendMail;
import com.yao.util.SendSms;
import com.yao.mapper.UserMapper;
import com.yao.pojo.User;
import com.yao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 妖悲冥
 * @date 2021/5/17 0017   -    15:53
 */

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    SendMail sendMail;
    @Autowired
    SendSms sendSms;
    @Autowired
    SaveFile saveFile;

    @Override
    public User login(User user) {
        return userMapper.selectOfLog(user.getUsername(), user.getPassword());
    }

    @Override
    public String registered(User user) {
        if (userMapper.registered(user) > 0) {
            return "ok";
        }
        return "error";
    }

    @Override
    public String retrieve(User user) {
        if (userMapper.retrieve(user.getPassword(),user.getPhone(),user.getMail()) > 0) {
            return "ok";
        }
        return "error";
    }

    @Override
    public String getCodeByMail(User user) {
        return sendMail.send(user.getMail());
    }

    @Override
    public String getCodeBySms(User user) {
        return sendSms.send(user.getPhone());
    }

    @Override
    public void setAvatar(MultipartFile file, String username) {
        saveFile.save(file, username);
    }

}
